<template>
  <div class="app-container">
    <el-form :inline="true" @submit.native.prevent>
      <el-button
        type="primary" plain
        icon="el-icon-upload2"
        size="mini"
        @click="handleImportCloudUp"
      >导入上云系统</el-button>
      <el-button
        type="primary" plain
        icon="el-icon-upload2"
        size="mini"
        @click="handleImportHost"
      >导入云主机</el-button>
      <el-button
        type="primary" plain
        icon="el-icon-upload2"
        size="mini"
        @click="handleImportOther"
      >导入其他资源</el-button>
    </el-form>

    <el-divider></el-divider>

    <row style="margin-top: 10px;">
      <el-button
        type="primary" plain
        icon="el-icon-upload2"
        size="mini"
        @click="handleImportCloudUpFiles"
      >上传系统附件</el-button>
    </row>

    <el-divider></el-divider>

    <row style="margin-top: 10px;">
      <el-button
        type="danger"
        icon="el-icon-refresh"
        size="mini"
        @click="handleClearCache"
      >清理缓存</el-button>
    </row>

    <el-divider></el-divider>

    <!-- 导入弹出框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        :accept="upload.accept"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或<em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <span class="el-upload__tip" style="color:red" slot="tip">
          <br>{{upload.info}}
          <el-link type="info" style="font-size:12px" @click="importTemplate(upload.pattern)">下载模板</el-link>
          </span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { clearCache } from "@/api/system/dict/type";
  import { getToken } from '@/utils/auth';
  import { importHostTemplateCloudUp, importHostTemplateEcs, importHostTemplateOther } from '@/api/basic/services';

  export default {
    name: "importExcel",
    components: {},
    data() {
      return {
        // 用户导入参数
        upload: {
          accept: ".xlsx, .xls",
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: '',
          info: "",//提示内容
          pattern: "cloudUp",
        },
      };
    },
    created() {;
    },
    methods: {
      /** 导入按钮操作 */
      handleImportCloudUpFiles() {
        this.upload.url = process.env.VUE_APP_BASE_API + "/cloudupfiles/importData",
        this.upload.title = "上云系统附件导入";
        this.upload.open = true;
        this.upload.info = "请将附件压缩为zip后上传，附件名格式：xxxx-xxx系统-xxx云服务商.pdf，单个压缩包内文件数量不能超过20个"
        this.upload.accept = ".zip";
        this.upload.pattern = null;
      },
      /** 导入按钮操作 */
      handleImportCloudUp() {
        this.upload.url = process.env.VUE_APP_BASE_API + "/importSystemData/importData",
        this.upload.title = "上云系统导入";
        this.upload.open = true;
        this.upload.info = "1，上云系统的部门如不存在，则放到其他部门（id：999）；" +
          "2，上云系统会根据项目名称匹配父级项目，如果父项目不存在则会进行创建"
        this.upload.accept = ".xlsx, .xls";
        this.upload.pattern = 'cloudUp';
      },
      /** 导入按钮操作 */
      handleImportHost() {
        this.upload.url = process.env.VUE_APP_BASE_API + "/importHostsData/importData",
        this.upload.title = "云主机导入";
        this.upload.open = true;
        this.upload.info = "支持导入云主机，物理服务器";
        this.upload.accept = ".xlsx, .xls";
        this.upload.pattern = 'ecs';
      },
      /** 导入按钮操作 */
      handleImportOther() {
        this.upload.url = process.env.VUE_APP_BASE_API + "/importOtherData/importData",
        this.upload.title = "其他资源导入";
        this.upload.open = true;
        this.upload.info = "导入gpu加速卡等资源";
        this.upload.accept = ".xlsx, .xls";
        this.upload.pattern = 'other';
      },

      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;

        this.$refs.upload.clearFiles();
        this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      },

      /** 清理缓存按钮操作 */
      handleClearCache() {
        this.$confirm('清除所有缓存会导致掉线，是否继续？', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return clearCache();
        }).then(() => {
          this.msgSuccess("清理成功");
        })
      },

      /** 下载模板操作 */
      importTemplate(pattern) {
        let fun;
        if('cloudUp'==pattern) {
          fun = importHostTemplateCloudUp;
        }else if('ecs'==pattern) {
          fun = importHostTemplateEcs;
        }else if('other'==pattern) {
          fun = importHostTemplateOther;
        }else {
          this.msgError("不支持下载模板");
        }
        fun().then(response => {
          this.download(response.msg);
        })
      },
    },
  };
</script>
<style scoped lang="less">
</style>
